<!doctype html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
	<head>
		<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
		<meta http-equiv="content-script-type" content="text/javascript">
		<meta http-equiv="content-style-type" content="text/css">
		<meta name="author" content="Heidi Rakels">
		<meta name="description" content="Sanaware Java Docking - Developer guide - Dock Model Encoders and Decoders">
		<link rel="stylesheet" type="text/css" href="../style.css">
		<title>How to Use Dock Model Encoders and Decoders</title>
	</head>
<body>

<div class="nextprev">
<nobr><a href="dockingpath.html">&laquo; prev</a></nobr>
<nobr><a href="drag.html">next &raquo;</a></nobr>
</div>

<span class="item"><a href="../main/index.html">Java Docking Home</a></span>&gt;
<span class="item"><a href="index.html">Developer Guide</a></span>&gt;
<span class="selected item">How to Use Dock Model Encoders and Decoders</span>

<h2>
How to Use Dock Model Encoders and Decoders
</h2>

<h3>
:: Saving your Workspace with Dock Model Encoders and Decoders ::
</h3>
<p>
When the application is stopped, you can save the state of your <a href="../javadoc/com/javadocking/model/DockModel.html" target="_blank">DockModel</a>.
When the application is restarted later, you can put the docks, dockables, visualizers, and docking paths 
in the same state as they were saved.
</p>

<a name="DockModelEncoder"><!-- --></a>
<h3>
:: Dock Model Encoder ::
</h3>
<p>
You can use a <a href="../javadoc/com/javadocking/model/codec/DockModelEncoder.html" target="_blank">
DockModelEncoder</a> to save your dock model:
<blockquote><pre>
		// Save the dock model.
		DockModelPropertiesEncoder encoder = new DockModelPropertiesEncoder();
		if (encoder.canSave(dockModel))
		{
			try
			{
				encoder.save(dockModel);
			}
			catch (Exception e)
			{
				System.out.println("Error while saving the dock model.");
				e.printStackTrace();
			}
		}
</pre></blockquote>
If you want to use this save methods, the dock model should have a source file path that is not null.
<blockquote><pre>
				String source = dockModel.getSource();
</pre></blockquote>
Otherwise you have to use the export methods. You can give a destination path to these methods:
<blockquote><pre>
				encoder.export(dockModel, destination);
</pre></blockquote>
</p>

<a name="DockModelDecoder"><!-- --></a>
<h3>
:: Dock Model Decoder ::
</h3>
<p>
You can use a <a href="../javadoc/com/javadocking/model/codec/DockModelDecoder.html" target="_blank">
DockModelDecoder</a> to reload your dock model.
You have to give the folowing things to the decoder:
<ul>
	<li>A mapping between the IDs of the dockables and the dockables.</li>
	<li>A mapping between the IDs of the owner windows and the owner windows.</li>
	<li>A mapping between the IDs of the visualizers and the visualizers.</li>
</ul>
<blockquote><pre>
		// Try to decode the dock model from file.
		DockModelPropertiesDecoder dockModelDecoder = new DockModelPropertiesDecoder();
		if (dockModelDecoder.canDecodeSource(SOURCE))
		{
			try 
			{
				// Create the map with the dockables, that the decoder needs.
				Map dockablesMap = new HashMap();
				for (int index = 0; index < dockables.length; index++)
				{
					dockablesMap.put( dockables[index].getID(), dockables[index]);
				}	
				for (int index = 0; index < buttonDockables.length; index++)
				{
					dockablesMap.put( buttonDockables[index].getID(), buttonDockables[index]);
				}			
								
				// Create the map with the owner windows, that the decoder needs.
				Map ownersMap = new HashMap();
				ownersMap.put(frameId, frame);
				
				// Create the map with the visualizers, that the decoder needs.
				Map visualizersMap = new HashMap();
				visualizersMap.put("maximizer", maximizer);
				visualizersMap.put("minimizer", minimizer);

				// Decode the file.
				dockModel = (FloatDockModel)dockModelDecoder.decode(SOURCE, dockablesMap, ownersMap, visualizersMap);
			}
			catch (FileNotFoundException fileNotFoundException){
				System.out.println("Could not find the file [" + SOURCE + "] with the saved dock model.");
				System.out.println("Continuing with the default dock model.");
			}
			catch (IOException ioException){
				System.out.println("Could not decode a dock model: [" + ioException + "].");
				ioException.printStackTrace();
				System.out.println("Continuing with the default dock model.");
			}
		}
</pre></blockquote>
</p>

<hr>

<br/>
The source code of the samples can be found in:
<br/><br/>
<table>
<tr>
	<td><a href="code/CodecExample.java.html">CodecExample</a></td><td>Shows the usage, the encoding, and the decoding of dock models.</td>
</tr>
<tr>
	<td><a href="code/WorkspaceExample.java.html">WorkspaceExample</a></td><td>Shows the encoding and decoding of dock models and docking paths.</td>
</tr>
</table>

<br/>
<hr>
<br/>

<div class="nextprevbottom">
<nobr><a href="dockingpath.html">&laquo; prev</a></nobr>
<nobr><a href="drag.html">next &raquo;</a></nobr>
</div>

</body>

</html>